﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace BTServer
{
    public partial class UpdatePatient : Form
    {
        public UpdatePatient()
        {
            InitializeComponent();
            refreshPatients();
        }

        private void refreshPatients()
        {
            dataGridView1.Rows.Clear();
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            heartdb db = new heartdb(conn);
            var query = from p in db.patient
                        join  dp in db.devicehaspatient on p.id equals dp.Patientid 
                        join d in db.device on dp.Deviceid equals d.id where d.Status == "radi"
                        select new {medilaNumber = p.MedicalNumber, firstName = p.FirstName, lastName = p.LastName,
                        dateBirth = p.BirthDate, personalId = p.PersonalId, deviceName = d.DeviceName, deviceId = d.id};
            foreach (var q in query)
            {
                dataGridView1.Rows.Add(new string[] {q.medilaNumber.ToString(), q.firstName, q.lastName, q.dateBirth.ToString(), 
                                        q.personalId, q.deviceName, q.deviceId.ToString()});
            }
            ConnectionPool.checkInConnection(conn);
        }
        private void refreshDevices()
        {
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            heartdb db = new heartdb(conn);
            var query = from d in db.device where d.Status == "slobodan" select d;
            foreach (var q in query)
            {
                cmbDevice.Items.Add((device)q);
            }
            cmbDevice.SelectedIndex = 0;
            ConnectionPool.checkInConnection(conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dateGrid = (DataGridView)sender;
            if (dataGridView1.SelectedRows.Count > 0)
            {
                lblFirstName.Text = dateGrid.SelectedRows[0].Cells[1].Value.ToString();
                lblLastName.Text = dateGrid.SelectedRows[0].Cells[2].Value.ToString();
                lblMedicalNumber.Text = dateGrid.SelectedRows[0].Cells[0].Value.ToString();
                lblPersonalId.Text = dateGrid.SelectedRows[0].Cells[4].Value.ToString();

                deviceOfSelectedPatient.DeviceName = dateGrid.SelectedRows[0].Cells[5].Value.ToString();
                deviceOfSelectedPatient.id = UInt32.Parse(dateGrid.SelectedRows[0].Cells[6].Value.ToString());

                cmbDevice.Items.Clear();
                cmbDevice.Items.Add(deviceOfSelectedPatient);
                refreshDevices();
            }
        }

        device deviceOfSelectedPatient = new device();
        private void button2_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            heartdb db = new heartdb(conn);
            device d = (device)cmbDevice.SelectedItem;
            if (d.id != deviceOfSelectedPatient.id)
            {

                device updateDevice = (from dev in db.device where dev.id == d.id select dev).First();
                updateDevice.Status = "radi";
                db.SubmitChanges();
                updateDevice = (from dev in db.device where dev.id == deviceOfSelectedPatient.id select dev).First();
                updateDevice.Status = "slobodan";
                db.SubmitChanges();
                devicehaspatient updateRel = (from dhp in db.devicehaspatient where dhp.Deviceid == deviceOfSelectedPatient.id select dhp).First();
                updateRel.Deviceid = d.id;
                db.SubmitChanges();
                cmbDevice.Items.Clear();
                refreshDevices();
                refreshPatients();
            }
        }

    }
}
